# -*- coding: utf-8 -*-

from wxapp.models import ServiceDetail
from wxapp.models import GoodsDetail
from wxapp.models import ShoppingCartDetail
from wxapp.models import UserDetail
# from wxapp.models import UserDetail


#-------------project bag---------------
from wxapp.base.log import logger
import shoppingcar_mgr
import goods_mgr

all_userName = set()


'''
需要给前端的数据前面加多c_标识，不能将重要的数据外泄出去
'''

class UserObj:
	global all_userName
	all_userID_list = all_userName
	def __init__(self,name):
		self.name = str(name)
		self.userObj = UserDetail.objects.get(user_name=self.name)
		if not self.name in UserObj.all_userID_list:
			logger.info('error:cannot found user name %s' % (str(self.name)))

	def get_userID(self):
		return int(self.userObj.user_id)

	def get_goodsName(self):
		return str(self.userObj.user_name)


	def get_myCart(self):
		myCart = []
		all_CartID = shoppingcar_mgr.all_cartID
		for ci in all_CartID:
			cartObj = shoppingcar_mgr.ShoppingCartObj(int(ci))
			if str(cartObj.get_userID()) == self.name:
				myCart.append(cartObj)
		return myCart

	def c_get_myCart(self):
		cart_list = []
		myCart = self.get_myCart()
		for cart in myCart:
			if not cart.get_goodsNum() == 0:
				cartInfo = {}
				cartInfo['id'] = int(cart.get_cartID())
				cartInfo['user_name'] = self.name
				cartInfo['goods_name'] = str(goods_mgr.GoodsObj(cart.get_goodsID()).get_goodsName())
				cartInfo['goods_price'] = float(cart.get_goodsPrice())
				cartInfo['goods_num'] = float(cart.get_goodsNum())
				cartInfo['total'] = float(cart.get_goodsPrice())*float(cart.get_goodsNum())
				cart_list.append(cartInfo)
		return cart_list


def get_all_userName():
	global all_userName
	all_user = UserDetail.objects.all().values('user_name')
	for user in all_user:
		all_userName.add(str(user['user_name']))
	return all_userName


def register(name):
	if name in get_all_userName():
		return
	obj = UserDetail(user_name = str(name))
	obj.save()





